/*
 * kleurvake.java
 *
 * Created on 2 mei 2008, 20:46
 */

package client.gui.controls.kleurschema;

import client.gui.*;
import client.gui.controls.scrabble.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

/**
 *
 * @author  Gonyo
 */
public class KleurVakje extends KleurObject {
    Panel p = new Panel();
    Vakje v;   
    
        Blokje b;

    
    public KleurVakje(Vakje v) {
        
        initComponents();
        this.v=v;
        //Titled borders
        TitledBorder title;
        title = BorderFactory.createTitledBorder(v.getType().name());
        this.setBorder(title);
        
        txt_Rood.setText(v.getBackground().getRed()+"");
        txt_Groen.setText(v.getBackground().getGreen()+"");
        txt_Blauw.setText(v.getBackground().getBlue()+"");
        
        lbl_Rood.setText("R:");
        lbl_Groen.setText("G:");
        lbl_Blauw.setText("B:");
        
        btn_Kleur.setText("Meer kleuren ...");
        btn_Kleur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kleurActionPerformed(evt);
            }
        });
        addListeners();
        this.setLayout(new FlowLayout());
        if(v.getType() == Vakje.Type.NORMAAL)
        {
          
            p.setBackground(v.getBackground());
            p.setPreferredSize(new Dimension (40,40));
            this.add(p);
        }
        else
            this.add(v);
               
        this.add(lbl_Rood);   
        this.add(txt_Rood);
        this.add(lbl_Groen);
        this.add(txt_Groen);
        this.add(lbl_Blauw);
        this.add(txt_Blauw);
        this. add(btn_Kleur);
        this.setPreferredSize(new Dimension (400,75));

    }
    
    public void refresh()
    {
        v.bepaalLayout();
        if(v.getType() == Vakje.Type.NORMAAL)
        {
            p.setBackground(v.getBackground());
        }
        txt_Rood.setText(v.getBackground().getRed()+"");
        txt_Groen.setText(v.getBackground().getGreen()+"");
        txt_Blauw.setText(v.getBackground().getBlue()+"");
    }
    
      

    private void addListeners() {
        txt_Rood.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusLost(java.awt.event.FocusEvent evt) {
                controleer(evt);
            }
        });
        txt_Groen.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusLost(java.awt.event.FocusEvent evt) {
                controleer(evt);
            }
        });
        txt_Blauw.addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusLost(java.awt.event.FocusEvent evt) {
                controleer(evt);
            }
        });
    }
    
    public void setNieuweKleur(Color c)
    {
        
        txt_Rood.setText(c.getRed()+"");
        txt_Groen.setText(c.getGreen()+"");
        txt_Blauw.setText(c.getBlue()+"");
        v.setBackground(new Color(Integer.parseInt(txt_Rood.getText()),Integer.parseInt(txt_Groen.getText()),Integer.parseInt(txt_Blauw.getText())));
        
        if(v.getType() == Vakje.Type.NORMAAL)
        {
            p.setBackground(c);
        }  
    }
    
      public void setNieuweFontKleur(Color c)
    {
        v.setForeground(c);
    }
    
    private void controleer(java.awt.event.FocusEvent evt) {
        if(Integer.parseInt(((JTextField)evt.getSource()).getText())>=0 &&Integer.parseInt(((JTextField)evt.getSource()).getText())<256) 
             v.setBackground(new Color(Integer.parseInt(txt_Rood.getText()),Integer.parseInt(txt_Groen.getText()),Integer.parseInt(txt_Blauw.getText())));
         if(v.getType() == Vakje.Type.NORMAAL)
        {
            p.setBackground(v.getBackground());
        }  
    }
    
     private void kleurActionPerformed(java.awt.event.ActionEvent evt) {
         Kleurkiezer kk =new Kleurkiezer(this); 
        kk.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        kk.pack();
        kk.setLocationRelativeTo(this);
        kk.toon();
       
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 166, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
